# @Version        : 1.0
# @Update Time    : 2025/3/14 14:58
# @File           : product_detail_view.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息

import flet as ft
from flet_core import CrossAxisAlignment

from services.product_service import ProductService


class ProductDetailView(ft.Column):
    def __init__(self, page: ft.Page, params: dict = None):
        super().__init__()
        self.page = page
        self.params = params
        self.product_service = ProductService()  # 初始化 ProductService
        self.quantity = 1
        self.selected_size = 0
        self.expand = True
        self.product_data = self.product_service.get_product_by_id(int(self.params["data_id"][0]))
        self.build_interface()

    def build_interface(self):
        # 图片轮播区域（支持手势缩放）
        image_carousel = ft.Stack(
            [
                ft.Container(
                    height=300,
                    image_src=self.product_data["image"],
                    image_fit=ft.ImageFit.COVER,
                    alignment=ft.alignment.center,
                ),
                ft.Container(
                    content=ft.Row(
                        [ft.CircleAvatar(radius=4, color=ft.colors.WHITE) for _ in range(3)],
                        alignment=ft.MainAxisAlignment.CENTER
                    ),
                    bottom=20,
                    left=0, right=0
                )
            ], height=320
        )

        # 商品信息区域
        info_section = ft.Container(
            content=ft.Column(
                [
                    self._build_title_section(),
                    ft.Divider(height=20),
                    self._build_sku_selector(),
                    ft.Divider(height=30),
                    self._build_highlight_features(),
                    self._build_detail_description(),
                    self._build_reviews()
                ], spacing=15
            ),
            padding=20,
            bgcolor=ft.colors.WHITE
        )

        # 底部操作栏（固定定位）
        footer = ft.Container(
            content=ft.Row(
                [
                    ft.IconButton(
                        icon=ft.icons.FAVORITE_BORDER,
                        selected_icon=ft.icons.FAVORITE,
                        style=ft.ButtonStyle(
                            color={
                                "": ft.colors.GREY_600,
                                "selected": ft.colors.RED_500
                            }
                        )
                    ),
                    ft.ElevatedButton(
                        "立即购买",
                        expand=True,
                        height=45,
                        style=ft.ButtonStyle(
                            shape=ft.RoundedRectangleBorder(radius=8),
                            bgcolor=ft.colors.GREEN_500
                        )
                    ),
                    ft.IconButton(
                        icon=ft.icons.SHOPPING_CART,
                        icon_size=28
                    )
                ], spacing=15
            ),
            padding=15,
            bgcolor=ft.colors.WHITE,
            border=ft.border.only(top=ft.border.BorderSide(1, ft.colors.GREY_200))
        )

        self.controls = [
            ft.ListView(
                expand=True,
                controls=[image_carousel, info_section],
                padding=ft.padding.only(bottom=80)
            ),
            footer
        ]

    def _build_title_section(self):
        # 获取商品数据
        product_data = self.product_data
        return ft.Row(
            [
                ft.Column(
                    [
                        ft.Text(product_data["title"], size=24, weight=ft.FontWeight.BOLD),
                        ft.Row(
                            [
                                ft.Icon(ft.icons.STAR, color=ft.colors.AMBER_500, size=18),
                                ft.Text(str(product_data["rating"]), size=16, color=ft.colors.GREY_600),
                                ft.Container(
                                    content=ft.Text("爆款热卖", color=ft.colors.RED_500),
                                    border=ft.border.all(1, ft.colors.RED_100),
                                    border_radius=8,
                                    padding=ft.padding.symmetric(horizontal=8)
                                )
                            ], spacing=10
                        )
                    ], expand=True
                ),
                ft.Column(
                    [
                        ft.Text(
                            product_data["price"], size=24, color=ft.colors.RED_600,
                            weight=ft.FontWeight.BOLD
                        ),
                        ft.Text(product_data["weight"], color=ft.colors.GREY_600),
                        ft.Container(
                            content=ft.Row(
                                [
                                    ft.Icon(ft.icons.LOCAL_SHIPPING, size=16),
                                    ft.Text("明日达", color=ft.colors.GREEN_600)
                                ], spacing=5
                            ),
                            padding=ft.padding.only(top=5)
                        )
                    ], alignment=ft.MainAxisAlignment.END
                )
            ], alignment=ft.MainAxisAlignment.SPACE_BETWEEN
        )

    def _build_sku_selector(self):
        # 动态生成规格选择按钮
        return ft.Column(
            [
                ft.Text("规格选择", color=ft.colors.GREY_800),
                ft.Row(
                    [
                        ft.ElevatedButton(
                            text=spec["size"],
                            style=ft.ButtonStyle(
                                color={"": ft.colors.BLACK},
                                bgcolor={"": ft.colors.GREY_200, "selected": ft.colors.BLUE_200}
                            ),
                            on_click=lambda e, i=i: self._select_size(i),
                            data=i,
                        ) for i, spec in enumerate(self.product_data["specifications"])
                    ], spacing=10
                )
            ], spacing=10
        )

    def _build_highlight_features(self):
        return ft.Column(
            [
                ft.Text("核心卖点", size=18, weight=ft.FontWeight.BOLD),
                ft.Column(
                    [
                        ft.Row(
                            [
                                ft.Icon(ft.icons.CHECK_CIRCLE, size=16, color=ft.colors.GREEN_500),
                                ft.Text("产地直采 48小时冷链直达", color=ft.colors.GREY_700)
                            ], spacing=8
                        ),
                        ft.Row(
                            [
                                ft.Icon(ft.icons.CHECK_CIRCLE, size=16, color=ft.colors.GREEN_500),
                                ft.Text("坏果包赔 无忧售后", color=ft.colors.GREY_700)
                            ], spacing=8
                        )
                    ], spacing=8
                )
            ], spacing=10
        )

    def _build_detail_description(self):
        return ft.Column(
            [
                ft.Text("商品详情", size=18, weight=ft.FontWeight.BOLD),
                ft.Tabs(
                    tabs=[
                        ft.Tab(text="图文详情"),
                        ft.Tab(text="规格参数"),
                        ft.Tab(text="质检报告")
                    ],
                    scrollable=True
                ),
                ft.Image(src="images/detail_1.jpg"),
                ft.Text(
                    "果肉饱满 香甜多汁\n自然成熟 现摘现发",
                    size=16, color=ft.colors.GREY_700
                )
            ], spacing=15
        )

    def _build_reviews(self):
        # 动态生成用户评价内容
        reviews = self.product_data["reviews"]
        review_widgets = [
            ft.Row(
                [
                    ft.CircleAvatar(radius=20),
                    ft.Column(
                        [
                            ft.Text(review["user"]),
                            ft.Row([ft.Icon(ft.icons.STAR, size=14) for _ in range(review["rating"])])
                        ], spacing=5
                    )
                ], spacing=10
            )
            for review in reviews
        ]
        return ft.Column(
            [
                ft.Row(
                    [
                        ft.Text("用户评价(2.5万+)", size=16, weight=ft.FontWeight.BOLD),
                        ft.Text("好评率 98%", color=ft.colors.GREEN_600)
                    ], alignment=ft.MainAxisAlignment.SPACE_BETWEEN
                ),
                ft.Column(review_widgets, spacing=10)
            ], spacing=15
        )

    def _select_size(self, index):
        self.selected_size = index
        self.update()
